﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using BLL;
using System.Text.RegularExpressions;
namespace PhongMachTu.GUI
{
    public partial class SuaThuoc : Form
    {
        public event EventHandler ButtonCT_Clicked;
        public int strMaKhamBenh=0;
        ChiTietKhamBenhBLL bllCTKhamBenh = new ChiTietKhamBenhBLL();
        public SuaThuoc()
        {
            InitializeComponent();
        }
        ThuocBLL bllThuoc = new ThuocBLL();
        DonViBLL bllDonVi = new DonViBLL();
        CachDungBLL bllCachDung = new CachDungBLL();
        int maCTKB;
        private void SuaThuoc_Load(object sender, EventArgs e)
        {
            try
            {
                cbThuoc.DataSource = bllThuoc.LayDSThuoc();
                cbThuoc.DisplayMember = "TenThuoc";
                cbThuoc.ValueMember = "MaThuoc";

                cbDonVi.DataSource = bllDonVi.LayDSDonVi();
                cbDonVi.DisplayMember = "TenDonVi";
                cbDonVi.ValueMember = "MaDonVi";

                cbCachDung.DataSource = bllCachDung.LayDSCachDung();
                cbCachDung.DisplayMember = "TenCachDung";
                cbCachDung.ValueMember = "MaCachDung";

                foreach (var item in bllCTKhamBenh.LayDSCTKB_TEN(MsgBox.ctkb.MaKhamBenh.Value, MsgBox.ctkb.MaThuoc, MsgBox.ctkb.MaDonVi))
                {
                    cbThuoc.SelectedValue = item.MaThuoc;
                    cbDonVi.SelectedValue = item.MaDonVi;
                    cbCachDung.SelectedValue = item.MaCachDung;
                    txtSoLuong.Text = item.SoLuong.ToString();
                    maCTKB = item.MaChiTietKhamBenh;
                    strMaKhamBenh = item.MaKhamBenh.Value;
                }
            }
            catch 
            {
                
           
            }
           
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {

                CHITIETKHAMBENH ct = new CHITIETKHAMBENH();
                ct.MaChiTietKhamBenh = maCTKB;
                ct.MaThuoc = cbThuoc.SelectedValue.ToString();
                ct.MaDonVi = cbDonVi.SelectedValue.ToString();
                Regex regex = new Regex("^[\\d]+$");
                if (regex.IsMatch(txtSoLuong.Text))
                {
                    ct.SoLuong = Convert.ToInt32(txtSoLuong.Text);
                }
                else
                {
                    MsgBox.Info("Vui lÒNG nhẬp sỐ !");
                }
                ct.MaCachDung = cbCachDung.SelectedValue.ToString();
                ct.MaKhamBenh = strMaKhamBenh;

                if (bllCTKhamBenh.suaChiTietKhamBenh(ct))
                {
                    MsgBox.Success("SỬA thành công !");
                    if (this.ButtonCT_Clicked != null)
                        this.ButtonCT_Clicked(sender, e);
                    this.Close();
                }

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
